package com.bjb.controller;

import com.bjb.config.FileDfsUtil;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashSet;

@Api
@RestController
@RequestMapping("/api/file")
public class FileController {

    @Resource
    private FileDfsUtil fileDfsUtil;

    @Resource
    private FastFileStorageClient fastFileStorageClient;

    /**
     * http://localhost:8081/swagger-ui.html
     * http://192.168.72.130/group1/M00/00/00/wKhIgl0n4AKABxQEABhlMYw_3Lo825.png
     */
    @ApiOperation(value="上传文件", notes="测试FastDFS文件上传")
    @PostMapping(value = "/uploadFile")
    public ResponseEntity<String> uploadFile (@RequestParam("file") MultipartFile file) throws IOException {
        String result ;
//        try{
//            String path = fileDfsUtil.upload(file) ;
//            if (!StringUtils.isEmpty(path)){
//                result = path ;
//            } else {
//                result = "上传失败" ;
//            }
//        } catch (Exception e){
//            e.printStackTrace() ;
//            result = "服务异常" ;
//        }
        String originalFilename = file.getOriginalFilename().
                substring(file.getOriginalFilename().
                        lastIndexOf(".") + 1);
        // 上传   （文件上传可不填文件信息，填入null即可）
        StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), originalFilename, null);
        result = storePath.getFullPath();
        return ResponseEntity.ok(result);
    }

    /**
     * 文件删除
     */
    @ApiOperation(value="删除文件", notes="测试FastDFS文件删除")
    @GetMapping("/deleteByPath")
    public ResponseEntity<String> deleteByPath (){
        String filePathName = "group1/M00/00/00/wKhIgl0n4AKABxQEABhlMYw_3Lo825.png" ;
        fileDfsUtil.deleteFile(filePathName);
        return ResponseEntity.ok("SUCCESS") ;
    }
}
